package com.hd.trans.framework.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.*
import androidx.fragment.app.DialogFragment
import com.hd.trans.R
import com.hd.trans.binding.visible
import com.hd.trans.databinding.DialogRenameBinding
import com.hd.trans.utils.InputUtils

class RenameDialog(val fileName: String?): DialogFragment() {
    lateinit var mDataBinding: DialogRenameBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置dialog样式
        setStyle(STYLE_NO_TITLE, R.style.BaseDialogStyle)
    }

    override fun onStart() {
        super.onStart()
        //可以在createView里一次写
        //设置dialog 宽高
        //val width = Resources.getSystem().displayMetrics.widthPixels
        dialog?.window!!.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        dialog?.window!!.setGravity(Gravity.CENTER)
        dialog?.setCanceledOnTouchOutside(false)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = LayoutInflater.from(context)
            .inflate(R.layout.dialog_rename, container, false)
        mDataBinding = DialogRenameBinding.bind(view)
        initView()
        initDialog()
        setListener()
        return view
    }

    val textWatcher by lazy {
        object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                mDataBinding.ivClear.visible = s.isNotEmpty()
                if(TextUtils.isEmpty(s.toString().trim())){
                    mDataBinding.btnRight.isEnabled = false
                    mDataBinding.btnRight.alpha = 0.6f
                }else{
                    mDataBinding.btnRight.isEnabled = true
                    mDataBinding.btnRight.alpha = 1.0f
                }
            }
        }
    }

    private fun initView() {
        mDataBinding.apply {
            etContent.addTextChangedListener(textWatcher)
            fileName?.let {
                etContent.setText(it)
                etContent.setSelection(it?.length) // 将光标移至文字末尾
            }
            etContent.postDelayed({
                InputUtils.showKeyBoard(etContent)
            },300)
        }
    }

    private fun initDialog() {
        dialog?.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) //设置背景为透明
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
    }

    var clickLeft: (() -> Unit)? = null
    var clickRight: ((name:String) -> Unit)? = null

    private fun setListener() {

        mDataBinding.btnLeft.setOnClickListener {
            dismiss()
            clickLeft?.invoke()
            InputUtils.hideKeyboard(mDataBinding.etContent)
        }
        mDataBinding.btnRight.setOnClickListener {
            dismiss()
            clickRight?.invoke(mDataBinding.etContent.text.toString())
            InputUtils.hideKeyboard(mDataBinding.etContent)
        }
        mDataBinding.ivClear.setOnClickListener {
            mDataBinding.etContent.setText("")
        }
    }
}